<?php

namespace app\api\controller\user;

use app\api\model\common\Coin;
use app\api\model\common\CoinModel;
use app\api\model\order\OrderEarningModel;
use app\api\model\user\UserAddressModel;
use app\api\model\user\UserAssetsLogModel;
use app\api\model\user\UserAssetsModel;
use app\api\request\user\UserAddressRequest;
use app\common\controller\Api;
use think\Loader;

class UserAssets extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * Notes: 钱包资产信息
     * User: 浪子膏
     * Date: 2023/11/1 10:25
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info()
    {
        $userAssetsModel = new UserAssetsModel();
        $info = $userAssetsModel->where('user_id', $this->auth->id)->find();
        if (empty($info)) {
            $this->error(__('That information no longer exists'));
        }
        $info['usdt'] = floatval($info['usdt']);
        $info['btc'] = floatval($info['btc']);
        $info['bch'] = floatval($info['bch']);
        $info['ltc'] = floatval($info['ltc']);
        $info['etc'] = floatval($info['etc']);
        $info['zec'] = floatval($info['zec']);
        $info['dash'] = floatval($info['dash']);
        $info['kas'] = floatval($info['kas']);

        $info['day_income'] = floatval($this->dayIncome());
        $info['sum'] = $this->sum($info);

        $this->dataOk($info);
    }

    /**
     * Notes: 算出总资产 这设计的有点 low 但是无关紧要了
     * User: cyy
     * Date: 2023/12/7 21:26
     * @param $info
     * @return float
     */
    public function sum($info): float
    {
        $btc = CoinModel::where('coin_type', 'BTC')->value('coin_price_usd');
        $bch = CoinModel::where('coin_type', 'BCH')->value('coin_price_usd');
        $ltc = CoinModel::where('coin_type', 'LTC')->value('coin_price_usd');
        $kas = CoinModel::where('coin_type', 'KAS')->value('coin_price_usd');
        $zec = CoinModel::where('coin_type', 'ZEC')->value('coin_price_usd');
        $dash = CoinModel::where('coin_type', 'DASH')->value('coin_price_usd');
        $etc = CoinModel::where('coin_type', 'ETC')->value('coin_price_usd');

        $sum = round(($info['btc'] * $btc) + ($info['bch'] * $bch) + ($info['ltc'] * $ltc) +
            ($info['kas'] * $kas) + ($info['zec'] * $zec) + ($info['dash'] * $dash) + ($info['etc'] * $etc) + $info['usdt'], 6);

        return $sum;

    }

    /**
     * Notes:  今日收益
     * User: 浪子膏
     * Date: 2023/11/1 11:07
     * @return float|int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function dayIncome()
    {
        $orderEarningsModel = new OrderEarningModel();
        $sta_time = strtotime(date('Y-m-d'));
        $end_time = $sta_time + 86399;
        $list = $orderEarningsModel->where('user_id', $this->auth->id)
            ->where('create_time', 'between time', [$sta_time, $end_time])->select();
        $day_usdt = 0;
        if (count($list) > 0) {
            foreach ($list as $item) {
                $coin_price_usd = CoinModel::where('id', $item['coin_id'])->value('coin_price_usd');
                if (!empty($coin_price_usd)) {
                    $day_usdt += $coin_price_usd * $item['real_price'];
                }
            }
        }

        return $day_usdt;
    }

    /**
     * Notes: 钱包币种详情
     * User: 浪子膏
     * Date: 2023/11/6 11:06
     */
    public function typeInfo()
    {
        $params = request()->post();
        $validate = Loader::validate(UserAddressRequest::class);
        if (!$validate->scene('typeInfo')->check($params)) $this->error($validate->getError());

        $userAssetsModel = new UserAssetsModel();
        $balance = $userAssetsModel->where('user_id', $this->auth->id)->value($params['type']);

        $userAssetsLogModel = new UserAssetsLogModel();
        //昨日收益
        $yesterday_money = $userAssetsLogModel->where('user_id', $this->auth->id)->where('money_type', $params['type'])
            ->where('create_time', 'yesterday')->where('be', 1)->sum('money');
        //历史总收益
        $sum_money = $userAssetsLogModel->where('user_id', $this->auth->id)->where('money_type', $params['type'])
            ->where('be', 1)->sum('money');
        //累计打款
        $sum_withdraw_money = $userAssetsLogModel->where('user_id', $this->auth->id)->where('money_type', $params['type'])
            ->where('be', 2)->sum('money');


        $this->dataOk([
            'balance' => $balance,
            'yesterday_money' => $yesterday_money,
            'sum_money' => $sum_money,
            'sum_withdraw_money' => $sum_withdraw_money,
        ]);
    }

    /**
     * Notes: 币种收益列表
     * User: 浪子膏
     * Date: 2023/11/6 11:37
     * @throws \think\exception\DbException
     */
    public function typeList()
    {
        $params = request()->post();
        $validate = Loader::validate(UserAddressRequest::class);
        if (!$validate->scene('typeList')->check($params)) $this->error($validate->getError());

        $orderEarningsModel = new OrderEarningModel();
        $list = $orderEarningsModel->where('user_id', $this->auth->id)->where('coin_type', $params['type'])
            ->order('create_time desc')->field('id,order_no,coin_type,electric_charge_day,real_price,earnings_date')
            ->paginate(input('page_size') ?? 15);

        $this->dataOk($list);
    }


    /**
     * Notes: 充值地址
     * User: cyy
     * Date: 2023/12/7 21:34
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function topUp()
    {
        $userAddressModel = new UserAddressModel();
        $info = $userAddressModel->where('user_id', $this->auth->id)->where('type', 'usdt')->find();
        if (empty($info)) {
            $this->error(__('That information no longer exists'));
        }

        $this->dataOk($info);

    }
}